package com.score.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.jdbc.StringUtils;
import com.score.mapper.ILeaveRequestMapper;
import com.score.pojo.ReturnEntry;
import com.score.pojo.TLeaveRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class LeaveRequestServiceImpl {

    @Autowired
    private ILeaveRequestMapper leaveRequestMapper;

    public ReturnEntry stuApplyLeave(TLeaveRequest leaveRequest) {
        leaveRequest.setBzrExamine("needexamine");
        leaveRequest.setExamineState("passing");
        int insert = leaveRequestMapper.insert(leaveRequest);
        if (insert == 1)
            return new ReturnEntry().ok("申请发送成功", leaveRequest);
        return new ReturnEntry(404, "申请发送失败", null);
    }

    public ReturnEntry teaGetLeaveApplyToPage(String position, String college, String className, Integer currentPage, Integer pageSize) {
        QueryWrapper<TLeaveRequest> wrapper = new QueryWrapper<>();
        switch (position) {
            case "headmaster":
                wrapper.eq(!StringUtils.isNullOrEmpty(className), "class_name", className)
                        .eq("bzr_examine", "needexamine")
                        .eq("examine_state", "passing");
                break;
            case "instructor":
                wrapper.eq(!StringUtils.isNullOrEmpty(college), "college", college)
                        .eq("bzr_examine", "pass")
                        .eq("fdy_examine", "needexamine")
                        .eq("examine_state", "passing");
                break;
            case "deputySecretary":
                wrapper.eq(!StringUtils.isNullOrEmpty(college), "college", college)
                        .eq("fdy_examine", "pass")
                        .eq("fsj_examine", "needexamine")
                        .eq("examine_state", "passing");
                break;
            case "dean":
                wrapper.eq(!StringUtils.isNullOrEmpty(college), "college", college)
                        .eq("fsj_examine", "pass")
                        .eq("yz_examine", "needexamine")
                        .eq("examine_state", "passing");
                break;
        }
        Page<TLeaveRequest> leaveRequests = leaveRequestMapper.selectPage(new Page<>(currentPage, pageSize), wrapper);
        List<String> leaveTime = new ArrayList<>();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        for (TLeaveRequest leaveRequest :
                leaveRequests.getRecords()) {
            String time = formatter.format(leaveRequest.getTimeOffStart()) + "至" + formatter.format(leaveRequest.getTimeOffEnd());
            leaveTime.add(time);
        }
        return new ReturnEntry().ok(leaveRequests, leaveTime);
    }

    public ReturnEntry stuGetLeave(String stuNum, String username) {
        QueryWrapper<TLeaveRequest> wrapper = new QueryWrapper<>();
        wrapper.eq(!StringUtils.isNullOrEmpty(stuNum), "stu_num", stuNum)
                .eq(!StringUtils.isNullOrEmpty(username), "username", username)
                .eq("examine_state", "passing");
        return new ReturnEntry().ok(leaveRequestMapper.selectList(wrapper), "请假信息");
    }

    public ReturnEntry teaAgreeApply(String position, TLeaveRequest leaveRequest) {
        if (!leaveRequest.getWhereabouts().substring(0, 3).equals("江西省") || leaveRequest.getLeavedays() > 7) {
            switch (position) {
                case "headmaster":
                    leaveRequest.setBzrExamine("pass");
                    leaveRequest.setFdyExamine("needexamine");
                    break;
                case "instructor":
                    leaveRequest.setFdyExamine("pass");
                    leaveRequest.setFsjExamine("needexamine");
                    break;
                case "deputySecretary":
                    leaveRequest.setFsjExamine("pass");
                    leaveRequest.setYzExamine("needexamine");
                    break;
                case "dean":
                    leaveRequest.setYzExamine("pass");
                    leaveRequest.setExamineState("pass");
                    break;
            }
        } else if (leaveRequest.getLeavedays() >= 3) {
            switch (position) {
                case "headmaster":
                    leaveRequest.setBzrExamine("pass");
                    leaveRequest.setFdyExamine("needexamine");
                    break;
                case "instructor":
                    leaveRequest.setFdyExamine("pass");
                    leaveRequest.setFsjExamine("needexamine");
                    break;
                case "deputySecretary":
                    leaveRequest.setFsjExamine("pass");
                    leaveRequest.setExamineState("pass");
                    break;
            }
        } else if (leaveRequest.getLeavedays() > 1) {
            switch (position) {
                case "headmaster":
                    leaveRequest.setBzrExamine("pass");
                    leaveRequest.setFdyExamine("needexamine");
                    break;
                case "instructor":
                    leaveRequest.setFdyExamine("pass");
                    leaveRequest.setExamineState("pass");
                    break;
            }
        } else if (leaveRequest.getLeavedays() == 1) {
            switch (position) {
                case "headmaster":
                    leaveRequest.setBzrExamine("pass");
                    leaveRequest.setExamineState("pass");
                    break;
            }
        } else {
            return new ReturnEntry().fail("数据无法审核", null);
        }
        int i = leaveRequestMapper.updateById(leaveRequest);
        if (i == 1)
            return new ReturnEntry().ok(leaveRequest, leaveRequest.getUsername(), "数据更新成功");
        return new ReturnEntry().fail("数据更新错误", null);
    }


    public ReturnEntry teaUnagreeApply(String position, TLeaveRequest leaveRequest) {
        switch (position) {
            case "headmaster":
                leaveRequest.setBzrExamine("unpass");
                leaveRequest.setExamineState("unpass");
                int i = leaveRequestMapper.updateById(leaveRequest);
                if (i == 1)
                    return new ReturnEntry().ok(leaveRequest, "班主任未同意请假");
                break;
            case "instructor":
                leaveRequest.setFdyExamine("unpass");
                leaveRequest.setExamineState("unpass");
                i = leaveRequestMapper.updateById(leaveRequest);
                if (i == 1)
                    return new ReturnEntry().ok(leaveRequest, "辅导员未同意请假");
                break;
            case "deputySecretary":
                leaveRequest.setFsjExamine("unpass");
                leaveRequest.setExamineState("unpass");
                i = leaveRequestMapper.updateById(leaveRequest);
                if (i == 1)
                    return new ReturnEntry().ok(leaveRequest, "副书记未同意请假");
                break;
            case "dean":
                leaveRequest.setYzExamine("unpass");
                leaveRequest.setExamineState("unpass");
                i = leaveRequestMapper.updateById(leaveRequest);
                if (i == 1)
                    return new ReturnEntry().ok(leaveRequest, "院长未同意请假");
                break;
        }
        return new ReturnEntry().fail("请假信息无法更新", leaveRequest);
    }

    public ReturnEntry stuGetUnpass(String stuNum, String username) {
        QueryWrapper<TLeaveRequest> wrapper = new QueryWrapper<>();
        wrapper.eq(!StringUtils.isNullOrEmpty(stuNum), "stu_num", stuNum)
                .eq(!StringUtils.isNullOrEmpty(username), "username", username)
                .eq("examine_state", "unpass");
        List<TLeaveRequest> leaveRequests = leaveRequestMapper.selectList(wrapper);
        List<String> leaveTime = new ArrayList<>();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        for (TLeaveRequest leaveRequest :
                leaveRequests) {
            String time = formatter.format(leaveRequest.getTimeOffStart()) + "至" + formatter.format(leaveRequest.getTimeOffEnd());
            leaveTime.add(time);
        }
        return new ReturnEntry().ok(leaveRequests, leaveTime, "请假未通过信息");
    }

    public ReturnEntry stuGetUncancel(String stuNum, String username) {
        QueryWrapper<TLeaveRequest> wrapper = new QueryWrapper<>();
        wrapper.eq(!StringUtils.isNullOrEmpty(stuNum), "stu_num", stuNum)
                .eq(!StringUtils.isNullOrEmpty(username), "username", username)
                .ne("examine_state", "unpass")
                .ne("examine_state", "passing")
                .ne("examine_state", "cancel");
        List<TLeaveRequest> leaveRequests = leaveRequestMapper.selectList(wrapper);
        List<String> leaveTime = new ArrayList<>();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        for (TLeaveRequest leaveRequest :
                leaveRequests) {
            String time = formatter.format(leaveRequest.getTimeOffStart()) + "至" + formatter.format(leaveRequest.getTimeOffEnd());
            leaveTime.add(time);
        }
        return new ReturnEntry().ok(leaveRequests, leaveTime, "未销假请假信息");
    }

    public ReturnEntry stuGetCancel(String stuNum, String username) {
        QueryWrapper<TLeaveRequest> wrapper = new QueryWrapper<>();
        wrapper.eq(!StringUtils.isNullOrEmpty(stuNum), "stu_num", stuNum)
                .eq(!StringUtils.isNullOrEmpty(username), "username", username)
                .eq("examine_state","cancel");
        List<TLeaveRequest> leaveRequests = leaveRequestMapper.selectList(wrapper);
        List<String> leaveTime = new ArrayList<>();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        for (TLeaveRequest leaveRequest :
                leaveRequests) {
            String time = formatter.format(leaveRequest.getTimeOffStart()) + "至" + formatter.format(leaveRequest.getTimeOffEnd());
            leaveTime.add(time);
        }
        return new ReturnEntry().ok(leaveRequests, leaveTime, "已销假请假信息");
    }

    public ReturnEntry stuToCancel(TLeaveRequest leaveRequest) {
        leaveRequest.setExamineState("canceling");
        int i = leaveRequestMapper.updateById(leaveRequest);
        if (i == 1)
            return new ReturnEntry().ok(leaveRequest, "正在申请销假");
        return new ReturnEntry().fail(null, "申请销假失败");
    }

    public ReturnEntry teaGetUnpassToPage(String position, String college, String className, Integer currentPage, Integer pageSize) {
        QueryWrapper<TLeaveRequest> wrapper = new QueryWrapper<>();
        switch (position) {
            case "headmaster":
                wrapper.eq(!StringUtils.isNullOrEmpty(className), "class_name", className)
                        .eq("bzr_examine", "unpass");
                break;
            case "instructor":
                wrapper.eq(!StringUtils.isNullOrEmpty(college), "college", college)
                        .eq("fdy_examine", "unpass");
                break;
            case "deputySecretary":
                wrapper.eq(!StringUtils.isNullOrEmpty(college), "college", college)
                        .eq("fsj_examine", "unpass");
                break;
            case "dean":
                wrapper.eq(!StringUtils.isNullOrEmpty(college), "college", college)
                        .eq("yz_examine", "unpass");
                break;
        }
        Page<TLeaveRequest> leaveRequests = leaveRequestMapper.selectPage(new Page<>(currentPage, pageSize), wrapper);
        List<String> leaveTime = new ArrayList<>();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        for (TLeaveRequest leaveRequest :
                leaveRequests.getRecords()) {
            String time = formatter.format(leaveRequest.getTimeOffStart()) + "至" + formatter.format(leaveRequest.getTimeOffEnd());
            leaveTime.add(time);
        }
        return new ReturnEntry().ok(leaveRequests, leaveTime);
    }


    public ReturnEntry teaGetUncancel(String position, String college, String className, Integer currentPage, Integer pageSize) {
        QueryWrapper<TLeaveRequest> wrapper = new QueryWrapper<>();
        QueryWrapper<TLeaveRequest> eq = null;
        switch (position) {
            case "headmaster":
                eq = wrapper.eq(!StringUtils.isNullOrEmpty(className), "class_name", className)
                        .eq("bzr_examine", "pass")
                        .eq("fdy_examine", "unexamine");
                break;
            case "instructor":
                eq =wrapper.eq(!StringUtils.isNullOrEmpty(college), "college", college)
                        .eq("fdy_examine", "pass");
                break;
            case "deputySecretary":
                eq =wrapper.eq(!StringUtils.isNullOrEmpty(college), "college", college)
                        .eq("fsj_examine", "pass");
                break;
            case "dean":
                eq =wrapper.eq(!StringUtils.isNullOrEmpty(college), "college", college)
                        .eq("yz_examine", "pass");
                break;
            default:
                return new ReturnEntry().fail("获取失败", null);
        }
        eq.ne("examine_state", "unpass")
                .ne("examine_state", "passing")
                .ne("examine_state", "cancel");
        Page<TLeaveRequest> leaveRequests = leaveRequestMapper.selectPage(new Page<>(currentPage, pageSize), eq);
        List<String> leaveTime = new ArrayList<>();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        for (TLeaveRequest leaveRequest :
                leaveRequests.getRecords()) {
            String time = formatter.format(leaveRequest.getTimeOffStart()) + "至" + formatter.format(leaveRequest.getTimeOffEnd());
            leaveTime.add(time);
        }
        return new ReturnEntry().ok(leaveRequests, leaveTime);
    }

    public ReturnEntry teaGetCancel(String position, String college, String className, Integer currentPage, Integer pageSize) {
        QueryWrapper<TLeaveRequest> wrapper = new QueryWrapper<>();
        QueryWrapper<TLeaveRequest> eq = null;
        switch (position) {
            case "headmaster":
                eq = wrapper.eq(!StringUtils.isNullOrEmpty(className), "class_name", className)
                        .eq("bzr_examine", "pass");
                break;
            case "instructor":
                eq =wrapper.eq(!StringUtils.isNullOrEmpty(college), "college", college)
                        .eq("fdy_examine", "pass");
                break;
            case "deputySecretary":
                eq =wrapper.eq(!StringUtils.isNullOrEmpty(college), "college", college)
                        .eq("fsj_examine", "pass");
                break;
            case "dean":
                eq =wrapper.eq(!StringUtils.isNullOrEmpty(college), "college", college)
                        .eq("yz_examine", "pass");
                break;
            default:
                return new ReturnEntry().fail("获取失败", null);
        }
        eq.eq("examine_state", "cancel");
        Page<TLeaveRequest> leaveRequests = leaveRequestMapper.selectPage(new Page<>(currentPage, pageSize), eq);
        List<String> leaveTime = new ArrayList<>();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        for (TLeaveRequest leaveRequest :
                leaveRequests.getRecords()) {
            String time = formatter.format(leaveRequest.getTimeOffStart()) + "至" + formatter.format(leaveRequest.getTimeOffEnd());
            leaveTime.add(time);
        }
        return new ReturnEntry().ok(leaveRequests, leaveTime,"");
    }

    public ReturnEntry teaAgreeUncancel(TLeaveRequest leaveRequest) {
        leaveRequest.setExamineState("uncancel");
        int i = leaveRequestMapper.updateById(leaveRequest);
        if (i == 1)
            return new ReturnEntry().ok(leaveRequest, "老师不同意销假");
        return new ReturnEntry().fail(null, "处理错误");
    }

    public ReturnEntry teaAgreeCancel(TLeaveRequest leaveRequest) {
        leaveRequest.setExamineState("cancel");
        int i = leaveRequestMapper.updateById(leaveRequest);
        if (i == 1)
            return new ReturnEntry().ok(leaveRequest, "老师同意销假");
        return new ReturnEntry().fail(null, "处理错误");
    }



}
